package com.leyou.page.service;

import com.leyou.item.pojo.*;
import com.leyou.page.clients.BrandClient;
import com.leyou.page.clients.CategoryClient;
import com.leyou.page.clients.GoodsClient;
import com.leyou.page.clients.SpecificationClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GoodsService {

    @Autowired
    private BrandClient brandClient;

    @Autowired
    private CategoryClient categoryClient;

    @Autowired
    private GoodsClient goodsClient;

    @Autowired
    private SpecificationClient specificationClient;

    private static final Logger logger = LoggerFactory.getLogger(GoodsService.class);


    /**
     * 商品详情页   根据spuId 查询数据    页面展示
     * @param spuId
     * @return
     */
    public Map<String, Object> loadModel(Long spuId) {
        Map<String, Object> map = new HashMap<>();

        //查询spu
        Spu spu = this.goodsClient.querySpuById(spuId);
        map.put("spu", spu);

        //查询spuDetail
        SpuDetail spuDetail = this.goodsClient.querySpuDetailById(spuId);
        map.put("spuDetail", spuDetail);

        //查询skus
        List<Sku> skus = this.goodsClient.querySkuBySpuId(spuId);
        map.put("skus", skus);

        //查询商品三级分类,抽取方法
        List<Category> categories = getCategory(spu);
        map.put("categories", categories);

        //查询品牌
        List<Brand> brands = this.brandClient.queryBrandByBids(Arrays.asList(spu.getBrandId()));
        map.put("brand", brands.get(0));


        //处理规格参数的id:name对
        List<SpecParam> specParams = this.specificationClient.querySpecParam(null, spu.getCid3(), false, null);


        //获取所有品牌的规格模版组  与  组内参数
        Map<Long, String> specParamMap = new HashMap<>();
        specParams.forEach(spec -> {
            specParamMap.put(spec.getId(), spec.getName());
        });

        map.put("specParamMap", specParamMap);

        //获取规格参数
        List<SpecGroup> groups = this.specificationClient.querySpecGroups(spu.getCid3());

        map.put("groups", groups);

        return map;
    }


    /**
     * 抽取获取category name 的方法 根据 spu的三级分类id 查询商品分类名称
     * @param spu
     * @return
     */
    private List<Category> getCategory(Spu spu) {
        try {
            List<String> strings = this.categoryClient.queryNameByIds(Arrays.asList(spu.getCid1(), spu.getCid2(), spu.getCid3()));

            Category c1 = new Category();
            c1.setId(spu.getCid1());
            c1.setName(strings.get(0));

            Category c2 = new Category();
            c2.setId(spu.getCid2());
            c2.setName(strings.get(1));

            Category c3 = new Category();
            c3.setId(spu.getCid3());
            c3.setName(strings.get(2));
            return Arrays.asList(c1, c2, c3);
        } catch (Exception e) {
            logger.error("查询商品分类出错，spuId：{}", spu.getId(), e);
        }
        return null;
    }

}

